﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

// Additional Libraries
using System.IO.IsolatedStorage;
using System.Windows.Media.Imaging;
using System.IO;

namespace FindMe
{
    public partial class load : PhoneApplicationPage
    {
        BitmapImage bi = new BitmapImage();
        // Create a filename for JPEG file in isolated storage.
        String userJPEG = "userImage.jpg";

        public load()
        {
			InitializeComponent();
        }

        private void Storyboard1_Completed(object sender, EventArgs e)
        {
            IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

            if (settings.Contains("SessionToken"))
            {
                //Verifies for a saved FullName
                if (settings.Contains("FullName"))
                {
                   App.FullName= settings["FullName"].ToString();
                   MessageBox.Show("Last user was " + App.FullName);
                }
                
                //Verifies for saved contact image
                using (IsolatedStorageFile images = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (images.FileExists(userJPEG))
                    {
                        MessageBox.Show("yahoo! we have an image for that user!");

                            using (IsolatedStorageFileStream fileStream = images.OpenFile("userImage.jpg", FileMode.Open, FileAccess.Read))
                            {
                                bi.SetSource(fileStream);
                            }
                            App.UserImage = bi;        
                        }
                        
                    else
                    {
                        MessageBox.Show("Sorry, No UserImage found!");                    
                    }
                 }
                                
                
                Guid lastSessionToken = (Guid)settings["SessionToken"];
                if (lastSessionToken != new Guid())
                {
                  NavigationService.Navigate(new Uri(App.PathMain, UriKind.Relative));
                }
                else
                {

                    NavigationService.Navigate(new Uri(App.PathLogin, UriKind.Relative));
                }
            }

            else
            {

                NavigationService.Navigate(new Uri(App.PathLogin, UriKind.Relative));
            }
        }

        
    }
}